package com.hex.ds.hdtp.core.app.data.table.service.impl;

import com.hex.ds.hdtp.core.app.common.page.PageData;
import com.hex.ds.hdtp.core.app.common.page.converter.IPageAndPageDtoConverter;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.table.converter.ITableTransferFieldMapConverter;
import com.hex.ds.hdtp.core.app.data.table.dto.request.TableTransferFieldMapRequestDto;
import com.hex.ds.hdtp.core.app.data.table.dto.response.TableTransferFieldMapResponseDto;
import com.hex.ds.hdtp.core.app.data.table.service.ITableTransferFieldMapService;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.data.table.po.TableTransferFieldMapPo;
import com.hex.ds.hdtp.core.inf.data.table.repository.ITableTransferFieldMapRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Package com.hex.ds.hdtp.core.app.data.table.service.impl
 * @ClassName TableTransferFieldMapService
 * @Description 表迁移清单字段映射-业务接口实现
 * @Author Wang zhihao
 * @Date 2023/8/9 13:43
 * @Version v1.0
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TableTransferFieldMapService implements ITableTransferFieldMapService {

    private final ITableTransferFieldMapRepository tableTransferFieldMapRepository;

    private final ITableTransferFieldMapConverter converter;

    private final IPageAndPageDtoConverter pageAndPageDtoConverter;

    /**
     * @Method queryListByPage
     * @Param
     * @param tableTransferFieldMapRequestDto
     * @param pageDto
     * @Return com.hex.ds.hdtp.core.app.common.page.PageData
     * @Description 表迁移清单字段映射-分页查询
     * @Author Wang zhihao
     * @Date 2023/8/9 13:37
     * @Version V1.0
     */
    @Override
    public PageData queryListByPage(TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto, PageDto pageDto) {
        final Page<TableTransferFieldMapPo> pageResult = tableTransferFieldMapRepository.queryListByPage(
                converter.toTableTransferFieldMapPo(tableTransferFieldMapRequestDto), pageAndPageDtoConverter.toPage(pageDto));
        List<TableTransferFieldMapResponseDto> responseDtos = converter.toTableTransferFieldMapResponseDtoList(pageResult.getData());
        return new PageData(responseDtos, pageResult.getTotal());
    }

    /**
     * @Method add
     * @Param
     * @param tableTransferFieldMapRequestDto
     * @Return void
     * @Description 表迁移清单字段映射-新增
     * @Author Wang zhihao
     * @Date 2023/8/9 13:38
     * @Version V1.0
     */
    @Override
    public void add(TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto) {
        int count = tableTransferFieldMapRepository.add(converter.toTableTransferFieldMapPo(tableTransferFieldMapRequestDto));
        if (count != 1) {
            throw new RuntimeException("新增表迁移清单字段映射失败");
        }
    }

    /**
     * @Method modifyById
     * @Param
     * @param tableTransferFieldMapRequestDto
     * @Return void
     * @Description 表迁移清单字段映射-根据主键修改
     * @Author Wang zhihao
     * @Date 2023/8/9 13:40
     * @Version V1.0
     */
    @Override
    public void modifyById(TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto) {
        int count = tableTransferFieldMapRepository.modifyById(converter.toTableTransferFieldMapPo(tableTransferFieldMapRequestDto));
        if (count != 1) {
            throw new RuntimeException("修改表迁移清单字段映射失败");
        }
    }

    /**
     * @Method removeById
     * @Param
     * @param pkId
     * @Return void
     * @Description 表迁移清单字段映射-根据pkId删除
     * @Author Wang zhihao
     * @Date 2023/8/9 10:46
     * @Version V1.0
     */
    @Override
    public void removeById(String pkId) {
        int count = tableTransferFieldMapRepository.removeById(pkId);
        if (count != 1) {
            throw new RuntimeException("删除表迁移清单字段映射失败");
        }
    }

    /**
     * @Method batchAdd
     * @Param
     * @param tableTransferFieldMapRequestDtos
     * @Return void
     * @Description 批量新增
     * @Author Wang zhihao
     * @Date 2023/8/15 12:03
     * @Version V1.0
     */
    @Override
    public void batchAdd(List<TableTransferFieldMapRequestDto> tableTransferFieldMapRequestDtos) {
        for (TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto : tableTransferFieldMapRequestDtos) {
            add(tableTransferFieldMapRequestDto);
        }
    }

    /**
     * @Method removeByListPkId
     * @Param
     * @param listPkId
     * @Return void
     * @Description 根据表迁移清单主键删除
     * @Author Wang zhihao
     * @Date 2023/8/15 13:55
     * @Version V1.0
     */
    @Override
    public void removeByListPkId(String listPkId) {
        tableTransferFieldMapRepository.removeByListPkId(listPkId);
    }

    /**
     * @Method queryList
     * @Param
     * @param tableTransferFieldMapRequestDto
     * @Return java.util.List<com.hex.ds.hdtp.core.app.data.table.dto.response.TableTransferFieldMapResponseDto>
     * @Description 不分页查询
     * @Author Wang zhihao
     * @Date 2023/8/11 16:50
     * @Version V1.0
     */
    @Override
    public List<TableTransferFieldMapResponseDto> queryList(TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto) {
        List<TableTransferFieldMapPo> tableTransferFieldMap =
                tableTransferFieldMapRepository.queryList(converter.toTableTransferFieldMapPo(tableTransferFieldMapRequestDto));
        return converter.toTableTransferFieldMapResponseDtoList(tableTransferFieldMap);
    }

    /*
     * @Method: queryListByListPkId <br>
     * @Param: [tableTransferListPkId] <br>
     * @Return: java.util.List<com.hex.ds.hdtp.core.app.data.table.dto.response.TableTransferFieldMapResponseDto> <br>
     * @Description：根据表迁移清单主键查询该清单对应的表元数据映射<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/21 20:04 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public List<TableTransferFieldMapResponseDto> queryListByListPkId(String tableTransferListPkId) {
        TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto = new TableTransferFieldMapRequestDto();
        tableTransferFieldMapRequestDto.setListPkId(tableTransferListPkId);
        return queryList(tableTransferFieldMapRequestDto);
    }

    /**
     * @Method batchRemoveById
     * @Param
     * @param pkIds
     * @Return void
     * @Description 表迁移清单字段映射-批量删除
     * @Author Wang zhihao
     * @Date 2023/8/9 10:44
     * @Version V1.0
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void batchRemoveById(List<String> pkIds) {
        for (String pkId : pkIds) {
            removeById(pkId);
        }
    }

}
